﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InventoryManage : MonoBehaviour
{
    static InventoryManage instance;
   
    public GameObject bagPanel; // 背包面板
    public BagSaveGoods myBag;
    public GameObject slotGrid;
    public Slot slotPrefab;
    public Text itemInformation;
    public GameObject itemInformation_1111;
    public Image Image_left;
    public GameObject image_1111;
    public bool isimageO=false ;
    public GameObject MenuPanel;

    public Item [] FoodNum = new Item[6];
     
    void Awake()
    {
       
        if (instance != null)
            Destroy(this);
        instance = this;
        instance.myBag.itemList.Clear();
        for (int i = 0; i < 6; i++)
        {
            instance.FoodNum[i].itemHeld = 1;

        }
        instance.myBag.itemList.Add(instance.FoodNum[2]);
        instance.myBag.itemList.Add(instance.FoodNum[5]);
        instance.myBag.itemList.Add(instance.FoodNum[1]);
        instance.FoodNum[2].itemHeld = 3;
        instance.FoodNum[5].itemHeld = 4;
        instance.FoodNum[1].itemHeld = 1;

    }


    private void OnEnable()
    {
        RefreshItem();
        instance.itemInformation.text = "";
    }

    private void Update()
    {
        
        if (Input.GetKeyDown(KeyCode.I))
        {
         
            bagPanel.SetActive(!bagPanel.activeSelf);
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (Time.timeScale == 0) Time.timeScale = 1;
            else Time.timeScale = 0;
            MenuPanel.SetActive(!MenuPanel.activeSelf);
        }
    }
   
    public static  void ImageOpen()
    {

        instance.isimageO = true;
       instance. image_1111.SetActive(instance. isimageO);
        instance.itemInformation_1111.SetActive(instance.isimageO);
    }
    public static void UpadteItemInfo(string itemDescription)
    {
        instance.itemInformation.text = itemDescription;
    }
    public static void UpadteImageInfo(Sprite Image_Left)
    {
        instance.Image_left.sprite = Image_Left;
        instance.Image_left.transform.localScale = new Vector3(1f , 1f, 1f);
    }

    public static void CreateNewItem(Item item)
    {
        Slot newItem = Instantiate(instance.slotPrefab, instance.slotGrid.transform.position, Quaternion.identity);
       
        newItem.gameObject.transform.SetParent(instance.slotGrid.transform);
        newItem.SlotItem = item;
        newItem.SlotImage.sprite = item.itemImage;
        newItem.SlotNum.text = item.itemHeld.ToString();
        newItem.transform.localScale = new Vector3(1, 1, 1);
        
    }
    
    public static void RefreshItem()
    {
        for (int i = 0; i < instance.slotGrid.transform.childCount; i++)
        {
            if (instance.slotGrid.transform.childCount == 0) break;
            Destroy(instance.slotGrid.transform.GetChild(i).gameObject);
        }

        for (int i = 0; i < instance.myBag.itemList.Count; i++)
        {
            CreateNewItem(instance.myBag.itemList[i]);
        }
    }
}